import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import { useSelector } from 'react-redux';
import type { RootState } from '../store/store';

// 判断是否登录的函数（你可以根据实际情况修改）
// function isLogin() {
//   // 例如判断 localStorage 里有 token
//   return !!localStorage.getItem('phone') && !!localStorage.getItem('password');
// }

interface RBACGuardProps {
  deny?: string[];
  allow?: string[];
}

// 高阶组件：用于包裹需要守卫的页面
const WithAuthGuard: React.FC<RBACGuardProps> = ({ deny, allow }) => {
  const { isAuthenticated, role } = useSelector((state: RootState) => state.auth);
  
  if (!isAuthenticated) {
    // 未登录，跳转到登录页
    return <Navigate to="/" replace />;
  }
  if (deny && role && deny.includes(role)) {
    return <Navigate to="/guard" replace />;
  }
  if (allow && role && !allow.includes(role)) {
    // 不在允许列表
    return <Navigate to="/guard" replace />;
  }
  // 已登录，正常渲染页面
  return <Outlet />;
};

export default WithAuthGuard;